package com.sjq.project.web;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.FTPClientHelper;
import com.sjq.common.util.PageResults;
import com.sjq.common.web.SessionUserHelper;
import com.sjq.project.entity.ProjectFile;
import com.sjq.project.service.ProjectFileService;

@Controller
public class ProjectFileController {
	@Autowired
    private ProjectFileService fileService;
    @Autowired
    private HttpSession session;  
    private static final String INITURL = "file/"; 
   /**查询列表***/
   @RequestMapping(INITURL+"FileList")
   @ResponseBody
   public JSONObject  FileList(Integer pageNo, Integer pageSize, ProjectFile file){
 	  if(file==null){
    		file=new ProjectFile();
    	}
   	   pageNo = pageNo == null ? 1 : pageNo;
       PageResults<?> pageResults=fileService.findFileByPage(pageNo, PageResults.ALLPAGESIZE, file);
       JSONObject json=new JSONObject();
       json.put("dataList", pageResults.getResults());
       json.put("pageNo",  pageResults.getPageNo());
       json.put("pages", pageResults.getPageCount());
       return json;
   }
  
   /**提交表单---附件**/
    @RequestMapping(INITURL+"addProjectFile")
	 public ModelAndView addProjectFile(ProjectFile file,@RequestParam("fj") CommonsMultipartFile[]  fj) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       fileService.addFileFilesByFTP(file,fj, la);
       ModelAndView mav=new ModelAndView();
	   mav.setViewName("/view/result/success");
	   return mav;
    }
    
    /**通过ajax 提交附件**/
    @RequestMapping(INITURL+"addProjectFileAjax")
     @ResponseBody
	 public JSONObject addProjectFileAjax(ProjectFile file,@RequestParam("fj") CommonsMultipartFile[]  fj) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
 	   return  fileService.addFileFilesByFTP(file,fj, la);
    }
    /**百度webuploader上传**/
    @RequestMapping(INITURL+"addWebUploader")
    @ResponseBody
	public JSONObject addWebUploader(@RequestParam("file") CommonsMultipartFile[]  file,String[] size ,String[] name,String [] id) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       JSONObject json=new JSONObject();
       json.put("code", "200");
       json.put("msg", "ok");
       return  json;
    }
    /**下载附件1***/
    @RequestMapping(INITURL+"downFiles")
 	public ResponseEntity<byte[]>  downFiles(String id) throws IOException{
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       HttpHeaders headers=new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       String filename="1.jpg";
       headers.setContentDispositionFormData("attachment", filename);
       //读取FTP 上面的 附件
       FTPClientHelper ftp=new FTPClientHelper("\\000000\\");
       ResponseEntity<byte[]> nrs=new ResponseEntity<byte[]>(ftp.downFileByte("", "973a8d5687d741babca6a1f89f83b23d.jpg"), headers, org.springframework.http.HttpStatus.CREATED);
       return  nrs;
    }
 	//导出exl
    @RequestMapping(INITURL+"exportExcel")
 	public ResponseEntity<byte[]>  exportExcel(String id) throws IOException{
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       HttpHeaders headers=new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       String filename="1.jpg";
       headers.setContentDispositionFormData("attachment", filename);
       //读取FTP 上面的 附件
       FTPClientHelper ftp=new FTPClientHelper("\\000000\\");
       ResponseEntity<byte[]> nrs=new ResponseEntity<byte[]>(ftp.downFileByte("", "973a8d5687d741babca6a1f89f83b23d.jpg"), headers, org.springframework.http.HttpStatus.CREATED);
       return  nrs;
    }
 	
 	/**展示图片*/
    @RequestMapping(INITURL+"showPicture")
 	public void  showPicture(HttpServletResponse res) throws IOException{
 		res.setContentType("image/png");
 		OutputStream os=res.getOutputStream();
 	   //读取FTP 上面的 附件
        FTPClientHelper ftp=new FTPClientHelper("\\000000\\");
 		os.write(ftp.downFileByte("", "973a8d5687d741babca6a1f89f83b23d.jpg"));
    }

    /**删除信息**/
     @RequestMapping(INITURL+"deleteFile")
	 @ResponseBody
	 public JSONObject deleteFile(String fileId) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        return fileService.deleteFile(fileId,la);
    }	
    
    
    /**更新表单**/
     @RequestMapping(INITURL+"motifyFile")
	 @ResponseBody
	 public JSONObject motifyFile(ProjectFile file) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        return  fileService.updateFile(file,la);   
    }	
    
    
     /**
    * @description 根据查询条件查询表的所有详细信息
    **/
	@RequestMapping(INITURL+"findFileAllList")
    @ResponseBody
    public JSONObject  findFileAllList(ProjectFile file){
 		   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       	   LoginAccount la= helper.getLoginAccount();
		   return  fileService.findFileByListAll(file,la);  	
     }
   
   
}
